﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK_HTTT_06.BUS;

namespace PTTK_HTTT_06.GUI.other
{
    public partial class MHThemNhaCungCap : UserControl
    {
        int i = 0;
        public MHThemNhaCungCap()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i = dgvNguyenLieu.RowCount;
            i--;
            if (txtTenNCC.Text.ToString() != "" && txtDiaChi.Text.ToString() != "" && txtSoDienThoai.Text.ToString() != "")
            {
                if (HopLe(dgvNguyenLieu, txtTenNCC.Text.ToString(), i) == 1)
                {
                    DataGridViewRow _row = new DataGridViewRow();
                    dgvNguyenLieu.Rows.Add(_row);
                    dgvNguyenLieu.Rows[i].Cells[0].Value = txtTenNCC.Text.ToString();
                    dgvNguyenLieu.Rows[i].Cells[1].Value = txtDiaChi.Text.ToString();
                    dgvNguyenLieu.Rows[i].Cells[2].Value = txtSoDienThoai.Text.ToString();
                    i++;
                }
                else
                {
                    MessageBox.Show("Nhà cung cấp đã thêm bên dưới !");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin !");
                txtTenNCC.Focus();
            }
        }

        private int HopLe(DataGridView data, string str, int count)
        {
            int i = 0;
            while (i < count)
            {
                if (data.Rows[i].Cells[0].Value.ToString() == str)
                    return 0;
                i++;
            }
            return 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult _result = MessageBox.Show("Bạn có chắc muốn làm mới tất cả ?", "Xác nhận", MessageBoxButtons.OKCancel);
            if (_result == DialogResult.OK)
            {
                dgvNguyenLieu.Rows.Clear();
                txtTenNCC.Text = "";
                txtDiaChi.Text = "";
                txtSoDienThoai.Text = "";
                txtTenNCC.Focus();
            }
        }

        private void txtSoDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvNguyenLieu.Rows.Count > 1)
            {
                NHA_CUNG_CAP _nguyenLieu = new NHA_CUNG_CAP();
                int i = 0;
                while (dgvNguyenLieu.Rows.Count > 1)
                {
                    if (dgvNguyenLieu.Rows[i].Cells[0].Value == null)
                    {
                        dgvNguyenLieu.Rows.RemoveAt(i);
                    }
                    else
                    {
                        _nguyenLieu.TenNhaCungCap = dgvNguyenLieu.Rows[i].Cells[0].Value.ToString();
                        _nguyenLieu.DiaChi = dgvNguyenLieu.Rows[i].Cells[1].Value.ToString();
                        _nguyenLieu.SoDienThoai = dgvNguyenLieu.Rows[i].Cells[2].Value.ToString();
                        NHA_CUNG_CAP.ThemNhaCungCap(_nguyenLieu);
                        dgvNguyenLieu.Rows.RemoveAt(i);
                    }
                }
                if (dgvNguyenLieu.RowCount == 1)
                {
                    MessageBox.Show("Thêm thành công");
                }
            }
        }
    }
}
